#!/usr/bin/python
#  This is part of the Scopia data visualization example collection
#
#
#  Copyright (C) 2008  Scopia Visual Interfaces Systems 
#                      http://www.scopia.es
#
#  author: Alejandro Conty
#  contributors:
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.

import scopiavf.adaptors.glut
import scopiavf.engine.glcontext as glcontext
from scopiavf.font.prerender import PrerenderedText
import sys
import math
from numpy import *
from scopiavf.engine.rendertree import *

ESCAPE = '\033'

class Style(object):
    def drawQuad(self, context, quadnode):
        quad = array((( 0.5,  0.5, 0), (-0.5,  0.5, 0), (-0.5, -0.5, 0), ( 0.5, -0.5, 0)), float32)
        context.setColor(1.0, 0.6, 0.2)
        context.drawQuadArray(quad)
        quad[:,2] += 0.001 # to avoid overlaping
        text = PrerenderedText(context, 'hello world', quad*0.8)
        context.setColor(0.0, 0.0, 0.0)
        text.draw(context)

class MyQuad(RenderGeomNode):
    def renderAll(self): self.style.drawQuad(self.context, self)

class Manager( glcontext.GlContext ):
    def __init__( self, w, h):
        glcontext.GlContext.__init__(self, w, h )
        self.style = Style( )
        quad = MyQuad( self, self.style )
        camera = RenderRotCameraNode(self, self.style, quad, (0,0,0), 1.5, math.pi*0.1 )
        self.root = RenderViewportNode(self, self.style, camera, 0, 0, 1, 1)
        camera.startRotation(0.2)

    def display(self):
        "Display function"
        self.depthTest( True )
        self.root.render()

    def keyboard(self, key, x, y):
        if key == ESCAPE:
            print 'Done'
            sys.exit()
    
adaptor = scopiavf.adaptors.glut.glutAdaptor_t(640, 480, lambda w, h: Manager(w, h) )
adaptor.loop()
